package demo;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.util.Duration;                 
import javafx.animation.Timeline;
import javafx.animation.FadeTransition;
import javafx.animation.TranslateTransition; 
import javafx.animation.RotateTransition;    
import javafx.animation.ScaleTransition;     
import javafx.animation.ParallelTransition;  

// 平行动画(演示多种动画，动画之间不干扰)
public class ParallelTransitionDemo extends Application { 
	private Pane root = new Pane();
	private Rectangle rectangle;
	private TranslateTransition translate;
	private RotateTransition rotate;
	private ScaleTransition scale;
	private FadeTransition fade;
	private ParallelTransition parallel;
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) {
		
	    rectangle = new Rectangle(40, 40, Color.CRIMSON);
	    rectangle.setArcWidth(20);
	    rectangle.setArcHeight(20);
	    rectangle.setLayoutX(50);
	    rectangle.setLayoutY(50);
	    root.getChildren().add(rectangle);
	    
		Scene scene = new Scene(root, 500, 400);
		stage.setScene(scene);
		stage.show();
		
		// 动画
		// 位移动画(直线移动)
		translate = new TranslateTransition(Duration.seconds(2), rectangle);
	    translate.setFromX(50);		// 以当前坐标为(0,0)
	    translate.setToX(320);
	    translate.setCycleCount(Timeline.INDEFINITE);
	    translate.setAutoReverse(true);
	    
	    // 旋转动画
	    rotate = new RotateTransition(Duration.seconds(2), rectangle);
	    rotate.setAxis(Rotate.Y_AXIS);	// 设置旋转轴(默认Z轴,可设置多个轴)
        rotate.setByAngle(180);			// 一个周期旋转的角度
        rotate.setAutoReverse(true);
        rotate.setCycleCount(Timeline.INDEFINITE);
//		rotate.setInterpolator(Interpolator.LINEAR);	// 插值器(匀速转动)
        
        // 缩放动画
        scale = new ScaleTransition(Duration.seconds(2), rectangle);
        scale.setToX(2);	// 宽高分别缩放的倍数
        scale.setToY(2);
        scale.setAutoReverse(true);
        scale.setCycleCount(Timeline.INDEFINITE);
        
        // 褪色动画
        fade = new FadeTransition(Duration.seconds(2), rectangle);
        fade.setFromValue(0.3);
        fade.setToValue(1);
        fade.setAutoReverse(true);
        fade.setCycleCount(Timeline.INDEFINITE);
     	    
        parallel = new ParallelTransition(translate, rotate, scale, fade);
        parallel.play();
	}
}



		
			
